import gugga.sequence.FictiveTask;
import gugga.common.ITask;

import test_framework.test.unit.TestCase;
import mx.utils.Delegate;

/**
 * @author vladimir
 */
class guggaLibTests.FictiveTaskTest
		extends TestCase
{
	private var isFictiveTaskStarted:Boolean;
	private var isFictiveTaskProgress:Boolean;
	private var isFictiveTaskCompleted:Boolean;
	
	private var isFictiveTaskInterrupted:Boolean;
	
	public function FictiveTaskTest()
	{
		super();
		this.isFictiveTaskStarted = false;
		this.isFictiveTaskProgress = false;
		this.isFictiveTaskCompleted = false;
		this.isFictiveTaskInterrupted = false;
	}
	
	public function testFictiveTask()
	{	
		var fictiveTask:ITask = new FictiveTask();
		
		fictiveTask.addEventListener(
				"start", 
				Delegate.create(this, this.onFictiveTaskStart));
		fictiveTask.addEventListener(
				"progress", 
				Delegate.create(this, this.onFictiveTaskProgress));
		fictiveTask.addEventListener(
				"completed", 
				Delegate.create(this, this.onFictiveTaskCompleted));
		
		fictiveTask.start();
		
		assertFictiveTaskStarted();
		assertFictiveTaskProgress();
		assertFictiveTaskCompleted();
	}
	
	public function testRepeatableStart()
	{
		var fictiveTask:ITask = new FictiveTask();
		
		fictiveTask.addEventListener(
				"start", 
				Delegate.create(this, this.onFictiveTaskStart));
		fictiveTask.addEventListener(
				"progress", 
				Delegate.create(this, this.onFictiveTaskProgress));
		fictiveTask.addEventListener(
				"completed", 
				Delegate.create(this, this.onFictiveTaskCompleted));
		
		fictiveTask.start();
		
		assertFictiveTaskStarted();
		assertFictiveTaskProgress();
		assertFictiveTaskCompleted();
		
		// try to start fictiveTask again:
		this.reset();
		fictiveTask.start();
		
		assertFictiveTaskStarted();
		assertFictiveTaskProgress();
		assertFictiveTaskCompleted();
	}
	
	public function testIsImmediatelyInterruptable()
	{
		var fictiveTask:ITask = new FictiveTask();
		
		assertTrue(
				"fictiveTask should be immediately interruptable", 
				fictiveTask.isImmediatelyInterruptable());
	}
	
	public function testInterrupt()
	{	
		var fictiveTask:ITask = new FictiveTask();
		
		fictiveTask.addEventListener(
				"start", 
				Delegate.create(this, this.onFictiveTaskStart));
		fictiveTask.addEventListener(
				"interrupted", 
				Delegate.create(this, this.onFictiveTaskInterrupted));
		
		fictiveTask.start();
		fictiveTask.interrupt();
		
		assertFictiveTaskStarted();
		assertFictiveTaskInterrupted();
	}
	
	private function assertFictiveTaskStarted()
	{
		assertTrue(
				"FictiveTask should be started right after start()", 
				this.isFictiveTaskStarted);
	}
	
	private function assertFictiveTaskProgress()
	{
		assertTrue(
				"FictiveTask should be in progress right after start()", 
				this.isFictiveTaskProgress);
	}
	
	private function assertFictiveTaskCompleted()
	{
		assertTrue(
				"FictiveTask should be completed right after start()", 
				this.isFictiveTaskCompleted);
	}
	
	private function assertFictiveTaskInterrupted()
	{
		assertTrue(
				"FictiveTask should be interrupted after interrupt()", 
				this.isFictiveTaskInterrupted);
	}
	
	private function onFictiveTaskStart()
	{
		this.isFictiveTaskStarted = true;
	}
	
	private function onFictiveTaskProgress(aEvent:Object)
	{
		this.isFictiveTaskProgress = true;
		
		assertEquals(
				"FictiveTask progress percents should be 100", 
				aEvent.percents, 
				100);
		assertEquals(
				"FictiveTask progress total should be 100", 
				aEvent.total, 
				100);
		assertEquals(
				"FictiveTask progress current should be 100", 
				aEvent.current, 
				100);
	}
	
	private function onFictiveTaskCompleted()
	{
		this.isFictiveTaskCompleted = true;
	}
	
	private function onFictiveTaskInterrupted()
	{
		this.isFictiveTaskInterrupted = true;
	}
	
	private function reset()
	{
		this.isFictiveTaskStarted = false;
		this.isFictiveTaskProgress = false;
		this.isFictiveTaskCompleted = false;
		this.isFictiveTaskInterrupted = false;
	}
}